//
//  DJAttestationzVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/6/3.
//

#import "DJAttestationzVC.h"
#import "TZImagePickerController.h"
#import "JYBDIDCardVC.h"

@interface DJAttestationzVC ()
@property (weak, nonatomic) IBOutlet YBTextFild *zName;
@property (weak, nonatomic) IBOutlet YBTextFild *zPhone;
@property (weak, nonatomic) IBOutlet YBTextFild *cName;
@property (weak, nonatomic) IBOutlet YBTextFild *sNumber;
@property (weak, nonatomic) IBOutlet YBTextFild *tTime;
@property (weak, nonatomic) IBOutlet UIImageView *sfz_qimage;
@property (weak, nonatomic) IBOutlet UIImageView *sfz_himage;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIButton *saoMiaoButt;
@property (weak, nonatomic) IBOutlet UIView *saoMiaoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *saoMiaoBotton;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipStatusHeight;
@property (weak, nonatomic) IBOutlet UIView *addQView;
@property (weak, nonatomic) IBOutlet UIView *addHView;
@property (weak, nonatomic) IBOutlet UIButton *addQButton;
@property (weak, nonatomic) IBOutlet UIButton *addHButton;

@end

@implementation DJAttestationzVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = NSLocalizedString(@"认证中心", nil);
    
    self.tipLabel.textColor = MainColor;
    
    self.zName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入你的真实姓名", nil) attributes:@{NSForegroundColorAttributeName:COLOR_ALPH(255, 255, 255, 0.82),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.zName.maxLength = 15;
    [self.zName addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.zPhone.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入你的手机号", nil) attributes:@{NSForegroundColorAttributeName:COLOR_ALPH(255, 255, 255, 0.82),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.zPhone.formaterType = YBAFormatterTextFieldFormatterTypePhone;
    [self.zPhone addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    self.cName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入认证专属名称", nil) attributes:@{NSForegroundColorAttributeName:COLOR_ALPH(255, 255, 255, 0.82),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.cName.maxLength = 15;
    [self.cName addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.sNumber.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入你的身份证号", nil) attributes:@{NSForegroundColorAttributeName:COLOR_ALPH(255, 255, 255, 0.82),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.sNumber.formaterType = YBAFormatterTextFieldFormatterTypeIDCard;
    [self.sNumber addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventEditingChanged];
    
//    self.tTime.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入有效日期（1980.01.01~2000.01.01）", nil) attributes:@{NSForegroundColorAttributeName:COLOR_ALPH(255, 255, 255, 0.82),NSFontAttributeName:[UIFont systemFontOfSize:15]}];
//    [self.tTime addTarget:self action:@selector(textFildChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.saoMiaoButt.backgroundColor = MainColor;
    self.saoMiaoButt.layer.cornerRadius = 12;
    self.saoMiaoButt.clipsToBounds = YES;
    
    if([[QZUserInfoModel share].creator_attestation count] != 0){
//        self.zName.text =
        if([[QZUserInfoModel share].creator_attestation[@"ischeck"] intValue] == 2){
            self.saoMiaoView.hidden = NO;
            self.saoMiaoBotton.constant = 15;
            self.tipStatusHeight.constant = 40;
            self.tipLabel.text = @"你的创作认证信息未通过，请重新上传认证信息";
            self.zName.text = [QZUserInfoModel share].creator_attestation[@"name"];
            self.cName.text = [QZUserInfoModel share].creator_attestation[@"creator_name"];
            self.sNumber.text = [QZUserInfoModel share].creator_attestation[@"idcard"];
            self.zPhone.text = [QZUserInfoModel share].creator_attestation[@"mobile"];
            [self.sfz_qimage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_front_image"])];
            [self.sfz_himage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_back_image"])];
            self.sfz_qimage.tag = 1;
            self.sfz_himage.tag = 1;
        }else if([[QZUserInfoModel share].creator_attestation[@"ischeck"] intValue] == 0){
            self.saoMiaoView.hidden = YES;
            self.saoMiaoBotton.constant = -55;
            self.saveButton.hidden = YES;
            self.tipStatusHeight.constant = 40;
            self.tipLabel.text = @"你的创作认证信息正在审核中，请等待审核";
            self.zName.text = [QZUserInfoModel share].creator_attestation[@"name"];
            self.zName.userInteractionEnabled = NO;
            self.cName.text = [QZUserInfoModel share].creator_attestation[@"creator_name"];
            self.cName.userInteractionEnabled = NO;
            self.sNumber.text = [QZUserInfoModel share].creator_attestation[@"idcard"];
            self.sNumber.userInteractionEnabled = NO;
            self.zPhone.text = [QZUserInfoModel share].creator_attestation[@"mobile"];
            self.zPhone.userInteractionEnabled = NO;
            [self.sfz_qimage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_front_image"])];
            [self.sfz_himage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_back_image"])];
            self.sfz_qimage.tag = 1;
            self.sfz_himage.tag = 1;
            self.addQView.hidden = YES;
            self.addHView.hidden = YES;
            self.addQButton.hidden = YES;
            self.addHButton.hidden = YES;
        }else if([[QZUserInfoModel share].creator_attestation[@"ischeck"] intValue] == 1){
            self.saoMiaoView.hidden = YES;
            self.saoMiaoBotton.constant = -55;
            self.saveButton.hidden = YES;
            self.tipStatusHeight.constant = 40;
            self.tipLabel.text = @"你的创作认证信息已经审核通过";
            self.zName.text = [QZUserInfoModel share].creator_attestation[@"name"];
            self.zName.userInteractionEnabled = NO;
            self.cName.text = [QZUserInfoModel share].creator_attestation[@"creator_name"];
            self.cName.userInteractionEnabled = NO;
            self.sNumber.text = [QZUserInfoModel share].creator_attestation[@"idcard"];
            self.sNumber.userInteractionEnabled = NO;
            self.zPhone.text = [QZUserInfoModel share].creator_attestation[@"mobile"];
            self.zPhone.userInteractionEnabled = NO;
            [self.sfz_qimage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_front_image"])];
            [self.sfz_himage sd_setImageWithURL:KUrlString([QZUserInfoModel share].creator_attestation[@"idcard_back_image"])];
            self.sfz_qimage.tag = 1;
            self.sfz_himage.tag = 1;
            self.addQView.hidden = YES;
            self.addHView.hidden = YES;
            self.addQButton.hidden = YES;
            self.addHButton.hidden = YES;
        }
    }else{
        self.tipLabel.text = @"";
    }
}

- (void)textFildChange:(UITextField *)textFild{
    
}

-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}
- (IBAction)addQImage:(id)sender {
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
        [leftButton setImage:[UIImage imageNamed:@"back_wite"] forState:UIControlStateNormal];
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 20)];
    }];
    imagePickerVc.allowTakePicture = YES;
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.needCircleCrop = YES;
    imagePickerVc.scaleAspectFillCrop = YES;
    imagePickerVc.circleCropRadius = KScreen_W/2;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.statusBarStyle = UIStatusBarStyleDefault;
    imagePickerVc.needShowStatusBar = YES;
    imagePickerVc.navigationBar.translucent = NO;
    imagePickerVc.iconThemeColor = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.naviBgColor = [UIColor whiteColor];
    imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
    imagePickerVc.barItemTextColor = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.oKButtonTitleColorNormal = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.naviTitleColor = [UIColor blackColor];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    KWeakSelf
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if(!photos.count){
            return;
        }
        weakSelf.sfz_qimage.image = photos[0];
        weakSelf.sfz_qimage.tag = 1;
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (IBAction)addHImage:(id)sender {
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
        [leftButton setImage:[UIImage imageNamed:@"back_wite"] forState:UIControlStateNormal];
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 20)];
    }];
    imagePickerVc.allowTakePicture = YES;
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.needCircleCrop = YES;
    imagePickerVc.scaleAspectFillCrop = YES;
    imagePickerVc.circleCropRadius = KScreen_W/2;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.statusBarStyle = UIStatusBarStyleDefault;
    imagePickerVc.needShowStatusBar = YES;
    imagePickerVc.navigationBar.translucent = NO;
    imagePickerVc.iconThemeColor = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.naviBgColor = [UIColor whiteColor];
    imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
    imagePickerVc.barItemTextColor = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.oKButtonTitleColorNormal = [UIColor colorWithHexString:@"#007AFF"];
    imagePickerVc.naviTitleColor = [UIColor blackColor];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    KWeakSelf
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if(!photos.count){
            return;
        }
        weakSelf.sfz_himage.image = photos[0];
        weakSelf.sfz_himage.tag = 1;
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (IBAction)saveTap:(id)sender {
    if(self.zName.text.length == 0){
        [YBMsgPop showPop:@"请输入你的真实姓名"];
        return;
    }
    if(self.zPhone.text.length == 0){
        [YBMsgPop showPop:@"请输入你的手机号"];
        return;
    }
    if(self.cName.text.length == 0){
        [YBMsgPop showPop:@"请输入认证名称"];
        return;
    }
    if(self.sNumber.text.length == 0){
        [YBMsgPop showPop:@"请输入你的身份证号码"];
        return;
    }
    if(self.sfz_qimage.tag != 1){
        [YBMsgPop showPop:@"请选择身份证正面照片"];
        return;
    }
    if(self.sfz_himage.tag != 1){
        [YBMsgPop showPop:@"请选择身份证反面照片"];
        return;
    }
    
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic)
    NSData *shenfenzhengFront = UIImageJPEGRepresentation(self.sfz_qimage.image, 0.8);
    NSData *shenfenzhengBack = UIImageJPEGRepresentation(self.sfz_himage.image, 0.8);
    sendDic[@"name"] = self.zName.text;
    sendDic[@"creator_name"] = self.cName.text;
    sendDic[@"mobile"] = [self.zPhone.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sendDic[@"idcard"] = self.sNumber.text;

    KWeakSelf
    [YBNetBase memberAccountAddAttestationRequest:sendDic shenfengzhengFrontImage:shenfenzhengFront shenfengzhengBackImage:shenfenzhengBack fuzhuImages:[NSArray new] successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [weakSelf requestUserHome];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)requestUserHome{
    KMutableDic(sendDic)
    KWeakSelf
    [YBNetBase userhomeRequest:sendDic isMy:YES successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            NSMutableDictionary * saveDatas = [[NSMutableDictionary alloc]initWithDictionary:data];
            [[QZUserInfoModel share] setValuesForKeysWithDictionary:saveDatas];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            [SVProgressHUDTool dismiss];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)showTextViewSubViewToViewTo2:(UIView * )subView{

    UIView * showTextViewSubViewTo2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo2.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo2];
    UILabel * showTextViewSubViewLabelTo3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo3.text = @"ppppp88888";
    [showTextViewSubViewTo2 addSubview:showTextViewSubViewLabelTo3];
    
    UIImageView * showTextViewSubViewTo3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo3];
}
- (IBAction)saoMiaoTap:(id)sender {
    if(sender == nil){
        JYBDIDCardVC *AVCaptureVC = [[JYBDIDCardVC alloc] init];
        AVCaptureVC.modalPresentationStyle = UIModalPresentationFullScreen;
        AVCaptureVC.scanType = 1;
        KWeakSelf
        AVCaptureVC.finish = ^(JYBDCardIDInfo *info, UIImage *image) {
            if (info.issue == nil || info.issue.length == 0 ){
                [YBMsgPop showPop:@"扫描身份证错误"];
                return;
            }
            weakSelf.sfz_himage.image = image;
            weakSelf.sfz_himage.tag = 1;
        };
        [self presentViewController:AVCaptureVC animated:YES completion:nil];
    }else{
        JYBDIDCardVC *AVCaptureVC = [[JYBDIDCardVC alloc]init];
        AVCaptureVC.modalPresentationStyle = UIModalPresentationFullScreen;
        AVCaptureVC.scanType = 0;
        KWeakSelf
        AVCaptureVC.finish = ^(JYBDCardIDInfo *info, UIImage *image) {
            if (info.name == nil || info.name.length == 0 ){
                [YBMsgPop showPop:@"扫描身份证错误"];
                return;
            }
            weakSelf.zName.text = info.name;
            weakSelf.sNumber.text = info.num;
            weakSelf.sfz_qimage.image = image;
            weakSelf.sfz_qimage.tag = 1;
            [weakSelf saoMiaoTap:nil];
        };
        [self presentViewController:AVCaptureVC animated:YES completion:nil];
    
    }

}

@end
